<template>
  <div class="content">
    <div class="header">
      <h3>用户列表</h3>
      <el-button type="primary" @click="foundUser">新建用户</el-button>
    </div>
    <!-- 表格 -->
    <div class="table">
      <el-table :data="userList" border style="width: 100%">
        <el-table-column align="center" type="index" label="序号" width="60" />
        <el-table-column align="center" prop="name" label="用户名" width="120" />
        <el-table-column align="center" prop="realname" label="真实姓名" width="100" />
        <el-table-column align="center" prop="cellphone" label="手机号码" width="150" />
        <el-table-column align="center" prop="enable" label="状态" width="90">
          <!-- 作用域插槽 -->
          <template #default="scope">
            <el-button type="primary" size="small" plain>
              {{ scope.row.enable ? '启用' : '禁用' }}
            </el-button>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="createAt" label="创建时间">
          <template #default="scope">
            {{ formatDay(scope.row.createAt) }}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="updateAt" label="更新时间">
          <template #default="scope">
            {{ formatDay(scope.row.updateAt) }}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="operate" label="操作" width="170">
          <template #default="scope">
            <el-button type="primary" icon="Edit" size="small" @click="editUserInfo(scope.row)"
              >编辑</el-button
            >
            <el-button type="danger" icon="Delete" size="small" @click="deleteUser(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页器 -->
    <div class="page">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[5, 10, 15]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalCount"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
import { ElMessage, ElMessageBox } from 'element-plus'
import useSystemStore from '@/store/modules/system'
import { storeToRefs } from 'pinia'
import { formatDay } from '@/utils/format'
import { ref } from 'vue'
import type { querListType } from '../type'
const emit = defineEmits(['handlerNewClick', 'handlerEditClick'])
const currentPage = ref(1)
const pageSize = ref(10)

// 发起action，请求userList的数据
const systemStore = useSystemStore()
// 因为是异步调用所以页面一刷新的时候可能是空的数据 所以需要 storeToRefs来变成响应式
// 当数据改变的时候  上面使用的数据就会自动刷新
postUserList()
const { userList, totalCount } = storeToRefs(systemStore)

function handleSizeChange() {
  postUserList()
}
function handleCurrentChange() {
  postUserList()
}
// 封装一个函数方便点击页码的时候调用
function postUserList(queryList?: querListType) {
  const size = pageSize.value
  const offset = (currentPage.value - 1) * size
  const Info = { size, offset }
  systemStore.postUserListData({ ...Info, ...queryList })
}
// 删除一个用户
function deleteUser(row: any) {
  ElMessageBox.confirm('确认删除吗', 'Warning', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
    closeOnClickModal: false
  })
    .then(() => {
      systemStore.deleteUsersInfo(row.id)
      ElMessage({
        type: 'success',
        message: '删除成功'
      })
    })
    .catch(() => {
      ElMessage({
        type: 'success',
        message: '取消成功'
      })
    })
}
// 新建用户
function foundUser() {
  emit('handlerNewClick')
}
function editUserInfo(userInfo: any) {
  emit('handlerEditClick', userInfo)
}

// 向父组件暴露出一个方法
defineExpose({
  postUserList
})
</script>

<style lang="less" scoped>
.content {
  padding: 20px;
  background-color: var(--el--page--search--bg--);
  border-radius: 10px;
}
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.table {
  margin-top: 20px;
  .el-table .el-table__cell {
    padding: 5px 0;
  }
  .el-table {
    z-index: 0;
  }
}

.page {
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
}
.el-overlay {
  :global(.el-overlay-message-box) {
    z-index: 99999;
    height: 100%;
    width: 100%;
    background: rgba(0, 0, 0, 0.2);
  }
}
</style>
